#include "../../../shared/dal/message_dao.hpp"
#include "../../../shared/dal/odb_factory.hpp"
#include "../../../entity/message.hxx"
#include "message-odb.hxx"
#include "logger.hpp"
#include <boost/date_time/posix_time/ptime.hpp>
#include <gflags/gflags.h>
#include <iostream>
#include <memory>
#include <string>
#include <gtest/gtest.h>

shared::dal::MessageDaoPtr messageDao;

boost::posix_time::ptime pt1 = boost::posix_time::time_from_string("2025-07-21 21:59:59.000");
boost::posix_time::ptime pt2 = boost::posix_time::time_from_string("2025-07-22 22:59:59.000");
boost::posix_time::ptime pt3 = boost::posix_time::time_from_string("2025-07-23 23:59:59.000");

TEST(TransmiteDBTest, Insert) 
{
    entity::Message msg1("msg1", "session-1", "uid1", 0, pt1);
    entity::Message msg2("msg2", "session-1", "uid2", 0, pt2);
    entity::Message msg3("msg3", "session-1", "uid3", 0, pt3);
    entity::Message msg4("msg4", "session-2", "uid4", 0, pt1);
    entity::Message msg5("msg5", "session-2", "uid5", 0, pt2);
    messageDao->insert(msg1);
    messageDao->insert(msg2);
    messageDao->insert(msg3);
    messageDao->insert(msg4);
    messageDao->insert(msg5);
}

TEST(TransmiteDBTest, selectRecent) 
{
    auto msgs = messageDao->selectRecentMessages("session-1", 2);
    ASSERT_EQ(msgs[0].messageId(), "msg3");
    ASSERT_EQ(msgs[0].sessionId(), "session-1");
    ASSERT_EQ(msgs[0].userId(), "uid3");
    ASSERT_EQ(msgs[0].messageType(), 0);
    ASSERT_EQ(boost::posix_time::to_simple_string(msgs[0].createTime()), boost::posix_time::to_simple_string(pt3));

    ASSERT_EQ(msgs[1].messageId(), "msg2");
    ASSERT_EQ(msgs[1].sessionId(), "session-1");
    ASSERT_EQ(msgs[1].userId(), "uid2");
    ASSERT_EQ(msgs[1].messageType(), 0);
    ASSERT_EQ(boost::posix_time::to_simple_string(msgs[1].createTime()), boost::posix_time::to_simple_string(pt2));
}

TEST(TransmiteDBTest, selectRange) 
{
    auto msgs = messageDao->selectRangeMessages("session-1", pt1, pt2);
    ASSERT_EQ(msgs[0].messageId(), "msg1");
    ASSERT_EQ(msgs[0].sessionId(), "session-1");
    ASSERT_EQ(msgs[0].userId(), "uid1");
    ASSERT_EQ(msgs[0].messageType(), 0);
    ASSERT_EQ(boost::posix_time::to_simple_string(msgs[0].createTime()), boost::posix_time::to_simple_string(pt1));

    ASSERT_EQ(msgs[1].messageId(), "msg2");
    ASSERT_EQ(msgs[1].sessionId(), "session-1");
    ASSERT_EQ(msgs[1].userId(), "uid2");
    ASSERT_EQ(msgs[1].messageType(), 0);
    ASSERT_EQ(boost::posix_time::to_simple_string(msgs[1].createTime()), boost::posix_time::to_simple_string(pt2));

}

TEST(TransmiteDBTest, Remove) 
{
    messageDao->remove("session-2");
    auto msgs = messageDao->selectRecentMessages("session-2", 10);
    ASSERT_TRUE(msgs.empty());
}

int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);

    auto db = shared::dal::ODBFactory::create("root", "weng9244", "127.0.0.1", "chat_system", "utf8", 0, 1);
    
    messageDao = std::make_shared<shared::dal::MessageDao>(db);

    return RUN_ALL_TESTS();
}